package com.zj.weather.bgviews.light.child;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;

import com.zj.weather.bgviews.light.LightInfo;
import com.zj.weather.bgviews.light.TentacleInfo;
import com.zj.weather.utils.DebugUtils;
import com.zj.weather.utils.DisplayUtils;
import com.zj.weather.utils.PathAliasChangeUtils;

import java.util.ArrayList;
import java.util.List;

import static com.zj.weather.bgviews.light.LightUtils.getEffectPaint;
import static com.zj.weather.utils.RandomUtils.getRandom;

/**
 * Created by zhaojie on 2018/8/14.
 */

public class MiddleLight extends LightInfo {
    private final static float maxStepInterval = 15, minStepInterval = 5;
    private final static float speed = 0.026f;
    private final static float maxTentacleSpeedOffset = speed * 0.25f, minTentacleSpeedOffset = speed * -0.25f;
    private final static float maxXDur = 5, minXDur = -5;
    private final static float maxXOffset = 10, minXOffset = -10;//
    private final static float maxTentacleWidth = 2f, minTentacleWidth = 0.1f;
    private final static float tentacleMaxAlpha = 0.45f;
    private final static int maxTentacleSize = 3;
    private BlurMaskFilter blurMaskFilter;
    private List<TentacleInfo> tentacles;
    //自增量，用于为电触设置唯一ID；
    private int curTentacleId = 0xf1;

    public MiddleLight(float width, float height, float cloudHeight, float maxAlpha, float speed, int branch, int maxShiningCount) {
        super(width, height, cloudHeight, maxAlpha, speed, branch, maxShiningCount);
    }

    @Override
    public void initData(Context context) {
        if (tentacles == null) tentacles = new ArrayList<>();
        blurMaskFilter = new BlurMaskFilter(DisplayUtils.dip2px(context, maxTentacleWidth), BlurMaskFilter.Blur.SOLID);
        float curX = getRandom(0.1f, 0.9f) * width;
        float curY = getRandom(0f, 0.3f) * cloudHeight;
        tentacles.add(getTentacleInfo(context, null, 1, curX, curY, height, maxTentacleWidth, tentacleMaxAlpha, speed + getRandom(minTentacleSpeedOffset, maxTentacleSpeedOffset), 0, curTentacleId += 1));
    }

    @Override
    public boolean draw(Context c, Canvas canvas, float changeAlpha) {
        boolean isEnd = false;
        if (buildTentacles(c, changeAlpha, canvas)) {
            boolean isDismiss = tentacles.size() == 0;
            if (!isDismiss) {
                for (TentacleInfo info : tentacles) {
                    if (info.alpha <= 0) {
                        info.clear();
                        removeList.add(info);
                    } else {
                        int alpha = (int) (info.alpha * 255f * changeAlpha);
                        DebugUtils.e("" + alpha);
                        info.draw(getPaint(c, alpha, info.width), info.width, minTentacleWidth, true, 0.0f, 1.0f, 0.01f, canvas);
                    }
                    info.alpha -= 0.05f;
                }
                tentacles.removeAll(removeList);
                removeList.clear();
            }
            isEnd = isDismiss;
        }
        return isEnd;
    }

    private final List<TentacleInfo> addList = new ArrayList();
    private final List<TentacleInfo> removeList = new ArrayList();

    private boolean buildTentacles(Context c, float changeAlpha, Canvas canvas) {
        if (tentacles != null && tentacles.size() > 0) {
            for (TentacleInfo info : tentacles) {
                if (info.curY >= cloudHeight * getRandom(0.6f, 1.6f)) {
                    return true;
                } else {
                    info.upDateParams(tentacles);
                    info.setNewPosition(DisplayUtils.dip2px(c, getRandom(minXDur, maxXDur)), getRandom(info.speed * 0.1f, info.speed * 1.2f) * cloudHeight);
                    info.draw(getPaint(c, (int) (info.alpha * 255f * changeAlpha), info.width), info.width, minTentacleWidth, true, 0.0f, 1.0f, 0.01f, canvas);
                    if (isAddTentacle(info)) {
                        addList.add(getTentacleInfo(c, info.allPath, info.layer + 1, info.curX, info.curY, height, info.width, info.alpha, speed + getRandom(minTentacleSpeedOffset, maxTentacleSpeedOffset), info.id, curTentacleId += 1));
                    }
                }
            }
            tentacles.removeAll(removeList);
            tentacles.addAll(addList);
            addList.clear();
            removeList.clear();
            return false;
        }
        return true;
    }


    //重写此方法，是为了重新定义雷电消失的时间阀值；
    private TentacleInfo getTentacleInfo(Context c, Path curPath, int layer, float curX, float curY, float height, float width, float alpha, float speed, int parent, int id) {
        return new TentacleInfo(curPath, layer, curX, curY, height, width, DisplayUtils.dip2px(c, getRandom(minXOffset, maxXOffset)), alpha, speed, parent, id) {
            @Override
            public void draw(Paint paint, float maxWidth, float minWidth, boolean isDrawSurplus, float start, float end, float duration, Canvas canvas) {
                PathAliasChangeUtils.PathClipInfo info = PathAliasChangeUtils.getAliasPathClip(start, end, duration, maxWidth, minWidth, false, ownPath);
                int size = info.pathChanges.size();
                for (int i = 0; i < size; i++) {
                    PathAliasChangeUtils.PathChangeInfo changeInfo = info.pathChanges.get(i);
                    paint.setStrokeWidth(changeInfo.width);
                    canvas.drawPath(changeInfo.path, paint);
                }
            }
        };
    }

    private Paint getPaint(Context context, int curAlpha, float width) {
        return getEffectPaint(context, curAlpha, width, minStepInterval, maxStepInterval, 0, blurMaskFilter, false);
    }


    private boolean isAddTentacle(TentacleInfo tentacleInfo) {
        boolean isLastBranch = tentacleInfo.curY < cloudHeight;
        float f = getRandom(0.0f, 10f);
        boolean isOdds = f < 1f;
        boolean isSizeOverflow = (tentacles.size() + addList.size() < maxTentacleSize);
        return isLastBranch && isOdds && isSizeOverflow;
    }
}